package com.wwwarehouse.taskcenter.custom_view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.taskcenter.R;

import static com.wwwarehouse.common.tools.ConvertUtils.dip2px;


/**
 * @创建者 chenchao.
 * @创建时间 2017/9/5 8:52
 * @描述 ${TODO}
 */

public class ColumnChartView extends View {

    private Paint mPaintc1;
    private Paint mPaintc2;
    private Paint mTextPaint;
    private Rect mTextBound = new Rect();
    public ColumnChartView(Context context) {
        super(context);
        init(context);
    }

    public ColumnChartView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ColumnChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private Context mContext;
    private void init(Context context) {
        mContext=context;
        mPaintc2 = new Paint();
        mPaintc2.setColor(context.getResources().getColor(R.color.common_color_c3_ffffff));
        mPaintc2.setAntiAlias(true);
        mPaintc2.setAlpha(127);
        mPaintc2.setStyle(Paint.Style.FILL);
        mPaintc2.setStrokeWidth(dip2px(mContext,80));

        mTextPaint = new Paint();
        mTextPaint.setTextSize(mContext.getResources().getDimension(R.dimen.common_dimen_s4_dp18));
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(context.getResources().getColor(R.color.common_color_c4_121417));
    }

    private int currentHeight = 0;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRect(0,(getHeight()-(getHeight()*currentHeight/mTotal)),dp2px(80),getHeight(),mPaintc1);
        mTextPaint.getTextBounds(currentHeight+"",0,(currentHeight+"").length(),mTextBound);
        canvas.drawText(currentHeight + "", getMeasuredWidth()/2 - mTextBound.width() / 2-1, (getHeight()-(getHeight()*currentHeight/mTotal+dp2px(15))), mTextPaint);
        LogUtils.showInfoLog("getWidth()"+getWidth()+"mTextBound"+mTextBound);
        if (currentHeight < mHeight) {
            currentHeight += 1;
            invalidate();
        } else {
            //绘制中心数字总和
//            canvas.drawText(mNum + "", getMeasuredWidth()/2 - mTextBound.width() / 2-1, (getHeight()-(getHeight()*currentHeight/mTotal)-dp2px(15)), mTextPaint);
            LogUtils.showInfoLog("getMeasuredWidth()"+getMeasuredWidth()+"getWidth()"+getWidth()+"mTextBound"+mTextBound);
        }


    }

    private int dp2px(float dpValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    private int mHeight;
    private int mTotal;
    private int mNum;
    private int mColor;
    public void setData(int heigh,int total,int num,int color){
        mHeight = heigh;
        mTotal = total;
        mNum = num;
        mColor = color;
        mPaintc1 = new Paint();
        mPaintc1.setColor(mColor);
        mPaintc1.setAntiAlias(true);
        mPaintc1.setAlpha(127);
        mPaintc1.setStyle(Paint.Style.FILL);
        mPaintc1.setStrokeWidth(dip2px(mContext,80));
        invalidate();
    }
}
